﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB;
using YT.Commons;
using YT.Modules ;

namespace YT.XH.NOSE
{
    public partial class frmNSMsgAnalysis : Form
    {
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        public frmNSMsgAnalysis()
        {
            InitializeComponent();
        }

        public frmNSMsgAnalysis(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }
        private void FillGrid( string strGridName )
        {
            string strSQL = string.Empty;
            strSQL = "select * from SmsInbox where convert( varchar(10), recvdate,20)>='{0}' and convert(varchar(10),recvdate,20)<='{1}' ";
            if (mstrSickRecID != "")
            {
                strSQL +=  " and SickRecID='"+ mstrSickRecID  +"'";
            }
            if (strGridName == dgvDtl.Name)
            {
                this.dgvDtl.Rows.Clear ();
                strSQL += " and MsgType='N' and ";
                strSQL += GetCondition();
                strSQL = string.Format(strSQL, datStDate.Value, datEdDate.Value);
                DataTable dtDtl = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtDtl.Rows)
                {
                    this.dgvDtl.Rows.Add();
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colUserName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToDateString(drRow["RecvDate"]), DateFormatOption.DateFmtYMD);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colTitle1.Index].Value = CvtFunc.ToString(drRow["Title1"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colTitle2.Index].Value = CvtFunc.ToString(drRow["Title2"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colTitle3.Index].Value = CvtFunc.ToString(drRow["Title3"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colTitle4.Index].Value = CvtFunc.ToString(drRow["Title4"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colMobileNo.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);
                    this.dgvDtl.Rows[this.dgvDtl.Rows.Count - 1].Cells[colSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                }
            }
            else if (strGridName == dgvQuest.Name)
            {
                this.dgvQuest.Rows.Clear();
                strSQL += " and MsgType='Q' ";
                strSQL = string.Format(strSQL, datStQuest.Value, datEdQuest.Value);
                DataTable dtDtl = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtDtl.Rows)
                {
                    this.dgvQuest.Rows.Add();
                    this.dgvQuest.Rows[this.dgvQuest.Rows.Count - 1].Cells[QuestSickName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                    this.dgvQuest.Rows[this.dgvQuest.Rows.Count - 1].Cells[QuestSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                    this.dgvQuest.Rows[this.dgvQuest.Rows.Count - 1].Cells[QuestMobileNo.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);
                    this.dgvQuest.Rows[this.dgvQuest.Rows.Count - 1].Cells[QuestMsg.Index].Value = CvtFunc.ToString(drRow["Message"]);
                    this.dgvQuest.Rows[this.dgvQuest.Rows.Count - 1].Cells[QuestDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToDateString(drRow["RecvDate"]), DateFormatOption.DateFmtYMD);
                }
            }
            else if (strGridName == dgvOther.Name)
            {
                this.dgvOther.Rows.Clear();
                strSQL += " and (MsgType!='Q' and MsgType!='N')  ";
                strSQL = string.Format(strSQL, datStOthers.Value, datEdOthers.Value);
                DataTable dtDtl = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtDtl.Rows)
                {
                    this.dgvOther.Rows.Add();
                    this.dgvOther.Rows[this.dgvOther.Rows.Count - 1].Cells[OtherSickName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                    this.dgvOther.Rows[this.dgvOther.Rows.Count - 1].Cells[OtherSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                    this.dgvOther.Rows[this.dgvOther.Rows.Count - 1].Cells[OtherMobileNo.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);
                    this.dgvOther.Rows[this.dgvOther.Rows.Count - 1].Cells[OtherMsg.Index].Value = CvtFunc.ToString(drRow["Message"]);
                    this.dgvOther.Rows[this.dgvOther.Rows.Count - 1].Cells[OtherDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToDateString(drRow["RecvDate"]), DateFormatOption.DateFmtYMD);
                }
            }
            else if (strGridName == dgvAll.Name)
            {
                this.dgvAll.Rows.Clear ();
                strSQL = string.Format(strSQL, datStAll.Value, datEdAll.Value);
                DataTable dtDtl = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtDtl.Rows)
                {
                    this.dgvAll.Rows.Add();
                    this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllSickName.Index].Value = CvtFunc.ToString(drRow["SickName"]);
                    this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllSickRecID.Index].Value = CvtFunc.ToString(drRow["SickRecID"]);
                    this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllMobileNo.Index].Value = CvtFunc.ToString(drRow["MobilePhone"]);
                    this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllMsg.Index].Value = CvtFunc.ToString(drRow["Message"]);
                    this.dgvAll.Rows[this.dgvAll.Rows.Count - 1].Cells[AllDate.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToDateString(drRow["RecvDate"]), DateFormatOption.DateFmtYMD);
                }
            }
        }


        private string GetCondition()
        {
            string strConditon = " 1=1 ";
            CheckedListBox chkList = null;
            for (int i = 0; i < 4; i++)
            {
                string strTmpCon = "";
                if (i == 0) chkList = chkList1;
                if (i == 1) chkList = chkList2;
                if (i == 2) chkList = chkList3;
                if (i == 3) chkList = chkList4;

                if (chkList != null)
                {
                    strTmpCon = "(1=1";
                    for (int index = 0; index <= chkList.Items.Count-1; index++)
                    {
                        if ( chkList.GetItemChecked(index)  )
                        {
                            if (index == 0) strTmpCon += " or title1='A' ";
                            if (index == 1) strTmpCon += " or title2='B' ";
                            if (index == 2) strTmpCon += " or title3='C' ";
                            if (index == 3) strTmpCon += " or title4='D' ";
                            if (index == 4) strTmpCon += " or title5='E' ";
                        }
                    }
                    strTmpCon = strTmpCon.Replace("1=1 or", "");
                    strTmpCon = strTmpCon.Replace("1=1", "");
                    if (strTmpCon.Length >= 4)
                        strTmpCon += ")";
                    else
                        strTmpCon ="";
                }
                if ( strTmpCon !="") strConditon += " and " + strTmpCon;
            }
            return strConditon;

        }



       

        private void frmNSMsgAnalysis_Load(object sender, EventArgs e)
        {
            this.Text = "短信分析";
            datStDate.Value = DateTime.Now.Date.ToShortDateString()    ;
            datEdDate.Value = DateTime.Now.Date.ToShortDateString ()  ;

            datStAll.Value = DateTime.Now.Date.ToShortDateString();
            datEdAll.Value = DateTime.Now.Date.ToShortDateString();

            datStOthers.Value = DateTime.Now.Date.ToShortDateString();
            datEdOthers.Value = DateTime.Now.Date.ToShortDateString();

            datStQuest.Value = DateTime.Now.Date.ToShortDateString();
            datEdQuest.Value = DateTime.Now.Date.ToShortDateString();
        }

        private void frmNSMsgAnalysis_Activated(object sender, EventArgs e)
        {
            DataFunctions.InsertIntoMsgTable();
            
        }

        private void chkList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnQry_Click(object sender, EventArgs e)
        {

            string strConnectionPara = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + DataFunctions.GetApplication() + "\\data.mdb'";
            System.Data.OleDb.OleDbConnection mcon = new System.Data.OleDb.OleDbConnection(strConnectionPara);
            mcon.Open();
            DataFunctions.InsertIntoMsgTable( mdbcon); 

            string strGridName ="";

            if (tabControl1.SelectedTab == tabPageNormal)
                strGridName = dgvDtl.Name;
            else if (tabControl1.SelectedTab == tabPageQuest)
                strGridName = dgvQuest.Name;
            else if (tabControl1.SelectedTab == tabPageDetail)
                strGridName = dgvAll.Name;
            else if (tabControl1.SelectedTab == tabPageOther)
                strGridName = dgvOther.Name;

            FillGrid( strGridName  ); 
        }

        private void dateTimeEx1_Load(object sender, EventArgs e)
        {

        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            string strSickRecID = "";
            DataGridViewRow dgvRow = null ;
            if (tabControl1.SelectedTab == tabPageNormal)
            {
                dgvRow = this.dgvDtl.CurrentRow;
            }
            else if (tabControl1.SelectedTab == tabPageDetail)
            {
                dgvRow = this.dgvAll.CurrentRow;
            }
            else if (tabControl1.SelectedTab == tabPageQuest)
            {
                dgvRow = this.dgvQuest.CurrentRow;
            }
            else if (tabControl1.SelectedTab == tabPageOther)
            {
                dgvRow = this.dgvOther.CurrentRow; 
            }

            if (dgvRow != null)
            {               
                strSickRecID = CvtFunc.ToString(dgvRow.Cells[0].Value);               
            }
            else if ( mstrSickRecID !=string.Empty ) 
            {
                strSickRecID = mstrSickRecID;
            }

                
            frmNSSendMsg frmMsg = new frmNSSendMsg( strSickRecID  );
            frmMsg.ShowDialog(); 
        }
    }
}
